﻿#include "MyQueue.h"

Queue::Queue() 
{
	front = rear = NULL;
	counter = 0;
}

Queue::~Queue() 
{ 
	char value[DATA_MAX_SIZE];
	while (!IsEmpty())
		Dequeue(value, DATA_MAX_SIZE);
}

bool Queue::IsEmpty() 
{
	if (counter)
		return false;
	else 
		return true;
}

void Queue::Enqueue(const char* x, int size) 
{
	if(x == NULL || size == 0)
	{
		cout << "Enqueue: Queue size is zero." << endl;
		return ;
	}
	
	Node* newNode = new Node;
	
	memset(newNode->data, 0, DATA_MAX_SIZE);
	memcpy(newNode->data, x, size);
	newNode->data_size = size;
	newNode->next = NULL;
	
	if (IsEmpty()) 
	{
		front = newNode;
		rear = newNode;
	}
	else 
	{
		rear->next = newNode;
		rear = newNode;
	}
	counter++;
}

int Queue::Dequeue(char* x, int leng) 
{
	int data_size = 0;

	if (IsEmpty()) 
	{
		cout<< "Error: the queue is empty." << endl;
		return 0;
	}
	else 
	{
		if(leng < front->data_size)
		{
			cout<< "Error: Recieve data lenght min." << endl;
			return 0;
		}
		
		memcpy(x, front->data, front->data_size);
		data_size = front->data_size;

		Node* nextNode = front->next;
		delete front;
		front = nextNode;
		counter--;
	}

	return data_size;
}


